package com.cari.oa.dao;

import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.cari.oa.domain.AttachmentEntity;
import com.cari.oa.util.StrUtils;

@Repository
public class AttachmentDao extends BaseDao<AttachmentEntity> {
	/**
	 * 分页查询附件信息
	 * @param uploadUser 	附件提交人的英文名
	 * @param attachName	附件名称
	 * @param path			附件存放路径
	 * @param objectid		附件所关联的对象(项目/费用)的ID
	 * @param type			附件所关联的对象类型  1：项目  2：费用
	 * @param page			
	 * @param size
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<AttachmentEntity> getPage(String uploadUser, String attachName, 
			String path, String objectId, String type, int page, int size){
		
		String hql = "from AttachmentEntity m where 1=1 ";
		
		if(StrUtils.isNotEmpty(uploadUser))
			hql += " and m.uploadUser = :uploadUser";
		
		if(StrUtils.isNotEmpty(attachName))
			hql += " and m.name like :attachname";
		
		if(StrUtils.isNotEmpty(path))
			hql += " and m.path like :path";
		
		if(StrUtils.isNotEmpty(objectId))
			hql += " and m.objectId = :objectid";
		
		if(StrUtils.isNotEmpty(type))
			hql += " and m.type = :type";
		
		hql += " order by m.createTime desc";
		
		Query query = getSession().createQuery(hql);
		
		if(StrUtils.isNotEmpty(uploadUser))
			query.setParameter("uploadUser", uploadUser);
		
		if(StrUtils.isNotEmpty(attachName))
			query.setParameter("attachname", "%" + attachName + "%");
		
		if(StrUtils.isNotEmpty(path))
			query.setParameter("path", "%" + path + "%");
		
		if(StrUtils.isNotEmpty(objectId))
			query.setParameter("objectid", objectId);
		
		if(StrUtils.isNotEmpty(type))
			query.setParameter("type", type);
		
		query.setFirstResult((page-1)*size).setMaxResults(size);
		
		return query.list();
	}
	
	/**
	 * 查询附件信息
	 * @author -- Heyyroup
	 * @param uploadUser 	附件提交人的英文名
	 * @param attachName	附件名称
	 * @param path			附件存放路径
	 * @param objectid		附件所关联的对象(项目/费用)的ID
	 * @param type			附件所关联的对象类型  1：项目  2：费用
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<AttachmentEntity> getAttachments(String uploadUser, String attachName, 
			String path, String objectId, String type){
		String hql = "from AttachmentEntity m where 1=1 ";
		
		if(StrUtils.isNotEmpty(uploadUser))
			hql += " and m.uploadUser = :uploadUser";
		
		if(StrUtils.isNotEmpty(attachName))
			hql += " and m.name like :attachname";
		
		if(StrUtils.isNotEmpty(path))
			hql += " and m.path like :path";
		
		if(StrUtils.isNotEmpty(objectId))
			hql += " and m.objectId = :objectid";
		
		if(StrUtils.isNotEmpty(type))
			hql += " and m.type = :type";
		
		hql += " order by m.createTime desc";
		
		Query query = getSession().createQuery(hql);
		
		if(StrUtils.isNotEmpty(uploadUser))
			query.setParameter("uploadUser", uploadUser);
		
		if(StrUtils.isNotEmpty(attachName))
			query.setParameter("attachname", "%" + attachName + "%");
		
		if(StrUtils.isNotEmpty(path))
			query.setParameter("path", "%" + path + "%");
		
		if(StrUtils.isNotEmpty(objectId))
			query.setParameter("objectid", objectId);
		
		if(StrUtils.isNotEmpty(type))
			query.setParameter("type", type);
		
		return query.list();
	}
	
	/**
	 * 查询符合条件的记录
	 * @param uploadUser 	附件提交人的英文名
	 * @param attachName	附件名称
	 * @param path			附件存放路径
	 * @param objectid		附件所关联的对象(项目/费用)的ID
	 * @param type			附件所关联的对象类型  1：项目  2：费用
	 * @return
	 */
	public long getCount(String uploadUser, String attachName, 
			String path, String objectId, String type){
		
		String hql = "select count(*) from AttachmentEntity m where 1=1 ";
		
		if(StrUtils.isNotEmpty(uploadUser))
			hql += " and m.uploadUser = :uploadUser";
		
		if(StrUtils.isNotEmpty(attachName))
			hql += " and m.name like :attachname";
		
		if(StrUtils.isNotEmpty(path))
			hql += " and m.path like :path";
		
		if(StrUtils.isNotEmpty(objectId))
			hql += " and m.objectId = :objectid";
		
		if(StrUtils.isNotEmpty(type))
			hql += " and m.type = :type";
		
		hql += " order by m.createTime desc";
		
		Query query = getSession().createQuery(hql);
		
		if(StrUtils.isNotEmpty(uploadUser))
			query.setParameter("uploadUser", uploadUser);
		
		if(StrUtils.isNotEmpty(attachName))
			query.setParameter("attachname", "%" + attachName + "%");
		
		if(StrUtils.isNotEmpty(path))
			query.setParameter("path", "%" + path + "%");
		
		if(StrUtils.isNotEmpty(objectId))
			query.setParameter("objectid", objectId);
		
		if(StrUtils.isNotEmpty(type))
			query.setParameter("type", type);
		
		Object object = query.uniqueResult();

		return Long.parseLong(object.toString());
	}
    
	/**
	* Title: getByOjectId
	* Description:获取某个对象的所有附件
	* @author    hjx 
	* @date       2013-3-13 
	* @param objectId
	* @return
	*/
	@SuppressWarnings("unchecked")
	public List<AttachmentEntity> getByObjectId(String objectId) {
		String hql = "from AttachmentEntity where objectId =:objectId  order by createTime";
    	Query query = getSession().createQuery(hql);
    	query.setString("objectId", objectId);
    	return query.list();
	}
}
